package me.numark;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import me.numark.fragments.ProgressDialogFragment;

/**
 * Created by tian on 14-9-20:下午10:16.
 */
public class BaseActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.defaultTheme);
    }

    protected ProgressDialogFragment progressDialogFragment;

    protected void showProgress() {
        showProgress(null, null);
    }

    protected void showProgress(int progressMessageId, int progressTitleId) {
        showProgress(getString(progressMessageId), getString(progressTitleId));
    }

    protected void showProgress(String progressMessage, String progressTitle) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        progressDialogFragment = ProgressDialogFragment.newInstance(progressTitle, progressMessage);
        progressDialogFragment.show(ft, "dialog");
    }

    protected void showProgress(String progressMessage) {
        showProgress(progressMessage, null);
    }

    protected void showProgress(int progressMessageId) {
        showProgress(getString(progressMessageId));
    }

    protected void dismissProgress() {
        if (progressDialogFragment != null) {
            progressDialogFragment.dismiss();
            progressDialogFragment = null;
        }
    }
}
